﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdSaveFileAs.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "IAction.h"
#include "IDocument.h"
#include "UiCommonDialog.h"
#include "GbmpFileSystem.h"
#include "NotificationUtils.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 


using namespace Sample;
using namespace gcmp;

REGISTER_COMMAND(CmdSaveFileAs)

CmdSaveFileAs::CmdSaveFileAs() : CommandBase(ID_CMD_SAVE_FILE_AS, true)
{

}

CmdSaveFileAs::~CmdSaveFileAs()
{

}

bool CmdSaveFileAs::IsVisible() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

gcmp::OwnerPtr<gcmp::IAction> CmdSaveFileAs::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    const std::wstring filter = L"Project(*.gac)";
    std::wstring filePathName = 
        UiCommonDialog::ShowSaveFileDialog(GBMP_TR(L"另存工程"), FileSystem::GetWorkingDirPath(), filter);
     if (filePathName.empty())
     {
         return nullptr;
     }
    gcmp::IDocument* pDocument = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"UI文档获取失败",L"GDMP", L"2023-09-30");
    
    DocumentSaveResult docSaveResult;
    DocumentSaveOptions docSaveOptions(false);
    if(!pDocument->SaveAs(filePathName, docSaveOptions, docSaveResult))
    {
        std::wstring messageBox = GBMP_TR(L"未知错误！");
        switch (docSaveResult.Code)
        {
        case DocumentSaveError::FilePathNotExist:
            messageBox = GBMP_TR(L"文件路径不存在！");
            break;
        case DocumentSaveError::InvalidFilePath:
            messageBox = GBMP_TR(L"无效的文件路径！");
            break;
        case DocumentSaveError::CanNotSaveMaterials:
            messageBox = GBMP_TR(L"无法保存材质文件！");
            break;
        default:
            break;
        }
        //强提示
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"GDMP"),
            messageBox, (int)UiCommonDialog::ButtonType::OK);
    }
    else
    {   
        //弱提示
        NotificationUtils::PostNotification(NotificationTypes::SuccessNotificationType,
            GBMP_TR(L"文件保存成功!"));
    }
    return nullptr;
}